package org.example.system.controller;

import org.example.system.entity.Comment;
import org.example.system.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/api/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @GetMapping
    public List<Comment> list() {
        return commentService.findAll();
    }

    @PostMapping
    public Map<String, Object> add(@RequestBody Comment comment) {
        Map<String, Object> result = new HashMap<>();
        int row = commentService.add(comment);
        result.put("success", row > 0);
        result.put("message", row > 0 ? "添加成功" : "添加失败");
        return result;
    }

    @PutMapping
    public Map<String, Object> update(@RequestBody Comment comment) {
        Map<String, Object> result = new HashMap<>();
        int row = commentService.update(comment);
        result.put("success", row > 0);
        result.put("message", row > 0 ? "修改成功" : "修改失败");
        return result;
    }

    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        int row = commentService.delete(id);
        result.put("success", row > 0);
        result.put("message", row > 0 ? "删除成功" : "删除失败");
        return result;
    }
} 